#include <soar.h>

int main(int argc, char *argv[])
{
    char host_addr[LONG_FILE_NM_SIZE+1];
    char *mailfrom="=?utf-8?B?562U5aSNOiDmm7TmlrAvb3B0L3NvYXIvY29tbW9uL3BocC9zb2FyLw==?=<zhou_arron@163.com>";
    char *mailto;
    char *passwd;
    char *body;
    char *a_attach[MAIL_MAX_ATTACH_NUM];
    FILE *fp;
    char *p;
    struct stat st;
    SMTP smtp;
    int i;
    int res;

    if(argc < 3)
    {
        printf("Usage: %s mailto smtp_passwd\n", argv[0]);
        return(-1);
    }
    mailto=argv[1];
    passwd=argv[2];

    if(!(fp=fopen("body.txt", "r")))
    //if(!(fp=fopen("body.htm", "r")))
    {
        printf("open body file fail\n");
        return(-1);
    }
    fstat(fileno(fp), &st);
    ALLOC_MULTI_DOERR(body, char, st.st_size+1,
            printf("alloc body fail\n"); return(-1));
    for(p=body; fgets(p, st.st_size, fp); p+=strlen(p));

    for(i=0; i<MAIL_MAX_ATTACH_NUM; i++)
        a_attach[i]=(char *)NULL;

    sig_ignore(SIGPIPE);
    ossl_init();

    sprintf(host_addr, "zhou_arron:%s@smtp.163.com", passwd);
    if((res=smtp_create(host_addr, &smtp)) == 0)
    {
        if((res=smtp_hello(smtp)) == 0)
        {
            a_attach[0]="Makefile";
            a_attach[1]="smtp";
            if((res=smtp_send(smtp, mailto,
                            mailfrom, NULL,
                            "Test", body, a_attach)) == 0)
                printf("smtp_send succeed\n");
            else
                printf("smtp_send fail, res:%d\n", res);
        }
        else
            printf("smtp_hello fail, res:%d\n", res);
        smtp_quit(smtp);

        //if((res=smtp_quick_send(smtp, mailto,
        //                mailfrom, (char *)NULL,
        //                subject, body, a_attach)) == 0)
        //    printf("smtp_quick_send succeed\n");
        //else
        //    printf("smtp_quick_send fail, res:%d\n", res);
    }
    else
        printf("smtp_create fail, res:%d\n", res);
    smtp_destroy(smtp);

    return(0);
}
